#
# with open("C:\\Users\\BUPT\\Desktop\\swc010\\这是我最后的波纹\\txt_gen\\sign.txt", "wr") as f:  # 打开文件
#     f.write("swc010")  # 读取文件
#     data = f.read()
#     if data == 'new':
#         print(True)
#     else:
#         print(False)
#     f.close()
#
#     # print("Test stop now!")
#     # client.send(b"Test stopped")
#     # subprocess.Popen('taskkill /f /t /im iperf3.exe', stdin=subprocess.PIPE, stdout=subprocess.PIPE,
#     #                  shell=True)
#     #
#     # bind_width = 30
#     # package_size = 10
#     # print("开始测试，测试带宽为：" + str(bind_width) + "MB/s，测量数据的大小为" + str(package_size) + "MB，（单位均为字节）")
#     # ps = subprocess.Popen(
#     #     '.\iperf3_2\iperf3_2.exe -c 10.112.125.3 -u -b ' + str(bind_width) + 'M -n ' + str(
#     #         package_size) + 'M',
#     #     stdin=subprocess.PIPE,
#     #     stdout=subprocess.PIPE,
#     #     shell=True)
#     # resultlist = getstdout(ps)
#     # l = len(resultlist)
#     # print(resultlist[l - 4])
#     # speed = str(resultlist[l - 4]).split("  ")[5]
#     # lost_rate = str(resultlist[l - 4]).split("  ")[7]
#     # print("当前测试平均速度：" + speed + "；" + "当前测试平均丢包率：" + lost_rate + "；")
#     # print("本次测量结束...等待下一次测量...")
import codecs
import locale
import subprocess

def getstdout(p):
    mylist = []
    while True:
        data = p.stdout.readline()
        if data == b'':
            if p.poll() is not None:
                break
        else:
            mylist.append(data.decode(codecs.lookup(locale.getpreferredencoding()).name))
    return mylist

bind_width = 30
package_size = 20
print("开始测试，测试带宽为：" + str(bind_width) + "MB/s，测量数据的大小为" + str(
    package_size) + "MB，（单位均为字节）")
ps = subprocess.Popen(
    '.\iperf3_2\iperf3_2.exe -c 10.112.168.186 -u -b ' + str(
        bind_width) + 'M -n ' + str(
        package_size) + 'M',
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
    shell=True)
resultlist = getstdout(ps)
l = len(resultlist)
print(resultlist)
speed = str(resultlist[l - 4]).split("  ")[5]
lost_rate = str(resultlist[l - 4]).split("  ")[7]
print("当前测试平均速度：" + speed + "；" + "当前测试平均丢包率：" + lost_rate + "；")
print("本次测量结束...等待下一次测量...")